<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/8/5
  Time: 21:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <script type="text/javascript" src="/js/jquery-3.4.1.js"></script>
    <script type="text/javascript">
      function testJSON(){
        $.getJSON(
                "JSONServlet",
                {"name":"user2"},
                function(result){
                    //js需要通过eval()函数，将返回值转为一个js能够识别的json对象
                    //JSON对象中只有一个
                    var jsonUser = eval(result.user);
                    alert(jsonUser.name + "---" + jsonUser.pwd);
                }
        )
      }

      function testJSON2(){
        $.getJSON(
                "JSONServlet",
                {"name":"user2"},
                function(result){
                  //js需要通过eval()函数，将返回值转为一个js能够识别的json对象
                  //JSON对象中有多个
                  var jsonUsers = eval(result);
                  $.each(jsonUsers,function(i,element){
                        alert(this.name + "---" + this.pwd);
                  });
                }
        )
      }

    </script>
    <title>index</title>
  </head>
  <body>
        <input type="button" value="检测单个对象" onclick="testJSON()"/>
        <input type="button" value="检测多个对象" onclick="testJSON2()"/>
  </body>
</html>
